<template>
  <div class="login">
    <div class="login-image">
      <el-image :src="background" :fit="fit"></el-image>
    </div>
    <div class="login-model">
      <div>后台管理系统</div>
      <el-form
        :model="form"
        status-icon
        :rules="rules"
        ref="form"
        label-width="100px"
        style="width: 260px"
        label-position="top"
      >
        <el-form-item label="" prop="name" class="whitelebel">
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="" prop="password" class="whitelebel">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="form.password"
            @keyup.enter.native="submitForm"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm"
            style="width: 100%; margin-top: 40px"
            :loading="loading"
            >登陆</el-button
          >
          <!-- <el-button @click="resetForm('form')">注册</el-button> -->
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  postApiApilogin,
  postAdminPricingSetNologin,
  postAdminPricingGet_FeshiJSAPI,
} from "@/api";
import { setStore } from "@/utils/localStorage.js";
import background from "@/assets/images/login.png";
export default {
  data() {
    return {
      background,
      loading: false,
      form: {
        name: "",
        password: "",
      },
      rules: {
        name: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  mounted() {
    // postGetscheme().then((res) => {
    //   console.log("h5跳转小程序", res);
    //   location.href = res.data.openlink;
    // });
    if (!localStorage.getItem("userinfo")) {
      this.fsJssdk();
    } else {
      this.$router.push({ name: "Container" });
    }
  },
  methods: {
    fslogin(code) {
      let obj = {
        code,
      };
      postAdminPricingSetNologin(obj)
        .then((res) => {
          setStore("userinfo", res.data);
          this.$store.commit("mutationsNavMenuList", res.data.roletop);
          let qxobj = {};
          let qx = res.data.roletop[0].list[0].qx;
          qx.forEach((element) => {
            qxobj[element.rolekey] = 1;
          });
          qxobj = JSON.stringify(qxobj);
          this.$router.push({
            name: res.data.roletop[0].list[0].unique,
            query: { qxobj },
          });
          this.loading = false;
        })
        .catch((err) => {
          let data = JSON.stringify(err);
          alert("err" + data + "code:" + code);
        });
    },
    // 飞书jssdk
    async fsJssdk() {
      let that = this;
      let obj = {
        url: "https://ssy.13yu.com/dist/",
      };

      window.h5sdk.ready(async () => {
        let data = await postAdminPricingGet_FeshiJSAPI(obj).then(
          (res) => res.data
        );

        window.h5sdk.config({
          appId: data.appId, // 必填，应用ID
          timestamp: data.timeStamp, // 必填，生成签名的时间戳，毫秒级
          nonceStr: data.nonceStr, // 必填，生成签名的随机串
          signature: data.signature, // 必填，签名
          jsApiList: [
            // 当前开发文档中的网页应用API 均无需填写该字段
          ],
          onSuccess: function (result) {
            tt.requestAuthCode({
              appId: data.appId,
              success: (info) => {
                that.fslogin(info.code);
              },
              fail: (error) => {
                let str = JSON.stringify(error);
                that.$message.warning(str);
              },
            });
            // 成功回调，可以在成功之后使用 tt.xx jsapi
          },
          onFail: function (error) {
            // 失败回调
            let str = JSON.stringify(error);
            that.$message.warning(str);
          },
        });
        //  lark.ready参数为回调函数，在环境准备就绪时触发
      });
    },
    submitForm() {
      this.loading = true;
      let form = this.form;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // postApiApilogin(form)
          //   .then((res) => {
          //     setStore("userinfo", res.data);
          //     this.$router.push({ name: "Container" });
          //   })
          //   .catch((err) => {
          //     this.$message.warning(err.msg);
          //     this.loading = false;
          //   });
          postApiApilogin(form)
            .then((res) => {
              setStore("userinfo", res.data);
              this.$store.commit("mutationsNavMenuList", res.data.roletop);
              let qxobj = {};
              let qx = res.data.roletop[0].list[0].qx;
              qx.forEach((element) => {
                qxobj[element.rolekey] = 1;
              });
              qxobj = JSON.stringify(qxobj);
              this.$router.push({
                name: res.data.roletop[0].list[0].unique,
                query: { qxobj },
              });
              this.loading = false;
            })
            .catch((err) => {
              this.$message.warning(err.msg);
              this.loading = false;
            });
        } else {
          this.loading = false;
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100vh;
  background: #f5f5f5;
  // background: linear-gradient(-90deg, #304156 0%, rgb(250, 250, 248));
  // background-image: url("../../assets/images/login.png");
  // background-repeat: no-repeat;
  // background-position: right top;
  // background-size: cover;
  // background-color: #efefef;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 500px;
  /deep/ .whitelebel .el-form-item__label {
    color: #304156;
    font-size: 18px;
  }
  /deep/ input {
    // background: rgba($color: #000000, $alpha: 0.3);

    color: #304156;
    // border-color:  #304156;
  }
  .login-model {
    border-radius: 4px;
    background-color: #ffffff;
    box-shadow: 0 0 10px 10px #eeeeee;
    margin-left: 100px;
    padding: 60px 40px;
    > div {
      font-size: 20px;
      font-weight: 800;
      text-align: center;
      // color: #304156;
      margin-bottom: 80px;
    }
  }
  .login-image {
    width: 800px;
  }
}
</style>